package com.zhihuishu.micro.course.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.abel533.mapper.Mapper;
import com.google.common.collect.Lists;
import com.zhihuishu.micro.course.dao.KnowLedgePointMapper;
import com.zhihuishu.micro.course.model.KnowledgePoint;
import com.zhihuishu.micro.course.service.KnowLedgePointService;
import com.zhihuishu.toolkit.helper.BeanHelper;
import com.zhihuishu.toolkit.jedis.JedisHelper.Handler;

/**
 * 知识点Service
 * 
 * @author 嵇鹏
 * @descption:
 * 
 * 
 */
@Service("knowLedgePointService")
public class KnowLedgePointServiceImpl extends BaseServiceImpl<KnowledgePoint> implements
        KnowLedgePointService {

    protected Logger logger = LoggerFactory.getLogger(getClass());
    

   

    @Resource
    private KnowLedgePointMapper knowLedgePointMapper;

    @Override
    public Mapper<KnowledgePoint> getMapper() {

        return knowLedgePointMapper;
    }

    @Override
    public List<Integer> saveKnowledgePoints(List<KnowledgePoint> knowledgePoints, final Integer lessonId,
            final Integer lessonVideoId) {
        List<Integer> integers = Lists.newArrayList();
        for (final KnowledgePoint knowledgePoint : knowledgePoints) {
            knowledgePoint.setIsDeleted(0);//默认为删除
            knowledgePoint.setCreateTime(new Date());//创建时间
            getMapper().insertSelective(knowledgePoint);// 保存知识点
            integers.add(knowledgePoint.getId());
            jedisExecutor.execute( new Handler() {
                @Override
                public void handle(Jedis jedis) {
                    if (lessonVideoId != null) {// 为小节
                        jedis.sadd("LESSON_VIDEO_KNOWLEDEG" + lessonVideoId, knowledgePoint.getId()
                                .toString());
                    }
                    if (lessonId != null && lessonVideoId == null) {// 为节
                        jedis.sadd("LESSON_KNOWLEDGE" + lessonId, knowledgePoint.getId().toString());
                    }

                    Map<String, String> knowLedgePointMap = BeanHelper.describe(knowledgePoint);
                    jedis.hmset("cc:knowledge:id:" + knowledgePoint.getId(), knowLedgePointMap);
                    jedis.zadd("cc:course:" + knowledgePoint.getCourseId() + ":knowledge",
                            knowledgePoint.getId(), knowledgePoint.getId().toString());
                }
            });
        }

        return integers;
    }

    @Override
    public List<KnowledgePoint> queryKnowLedgesByLidOrLVid(Integer lessonId, Integer lessonVideoId) {
       /* KnowledgePoint knowledgePoint = new KnowledgePoint();
        knowledgePoint.setIsDeleted(0);*/
        Example example=new Example(KnowledgePoint.class);
        Criteria criteria = example.createCriteria();
        criteria.andEqualTo("isDeleted",0);
        if (lessonVideoId != null) {
            //knowledgePoint.setLessonVideoId(lessonVideoId);
            criteria.andEqualTo("lessonVideoId", lessonVideoId);
        } else {
            //knowledgePoint.setLessonId(lessonId);
            criteria.andEqualTo("lessonId", lessonId).andIsNull("lessonVideoId");
            
        }
       
        //getMapper().selectByExample(example);
        //List<KnowledgePoint> knowledgePoints = getMapper().select(knowledgePoint);
        List<KnowledgePoint> knowledgePoints=getMapper().selectByExample(example);
        if (logger.isDebugEnabled()) {
            logger.debug("查询到的结果为{}",knowledgePoints);
        }
        return knowledgePoints;
    }

    @Override
    public void updateKnowledgePoint(KnowledgePoint knowledgePoint) {
        knowledgePoint.setUpdateTime(new Date());
        int affectCount = getMapper().updateByPrimaryKeySelective(knowledgePoint);
        if (logger.isDebugEnabled()) {
            logger.debug("更新的行数为{}", affectCount);
        }

    }

    @Override
    public void deleteKnowLedgePoint(final List<Integer> ids, final Integer lessonId,
            final Integer lessonVideoId, final Long courseId) {
        int affectCount = knowLedgePointMapper.deleteKnowLedgePoint(ids);
        if (logger.isDebugEnabled()) {
            logger.debug("被删除的条数为{}", affectCount);
        }
        jedisExecutor.execute( new Handler() {

            @Override
            public void handle(Jedis jedis) {
                String[] strings = new String[ids.size()];
                for (int i = 0; i < ids.size(); i++) {
                    strings[i] = ids.get(i).toString();
                }
                // 从集合中删除知识点id
                 long affectCount=jedis.zrem("cc:course:" + courseId + ":knowledge", strings);
                 if (logger.isDebugEnabled()) {
                    logger.debug("课程{}集合中删除知识点id{},影响的条数为{}",courseId,strings,affectCount);
                }
                if (lessonVideoId != null) {// 删除小节知识点
                    Long count=jedis.srem("LESSON_VIDEO_KNOWLEDEG" + lessonVideoId, strings);
                    if (logger.isDebugEnabled()) {
                        logger.debug("小节{}从缓存列表中删除的知识点{}影响的数量为{}",lessonVideoId,strings,count);
                    }
                } else {// 删除节只是点
                    Long count=jedis.srem("LESSON_KNOWLEDGE" + lessonId, strings);// 清除节中知识点
                    if (logger.isDebugEnabled()) {
                        logger.debug("节{}从缓存列表中删除的知识点{}影响的数量为{}",lessonId,strings,count);
                    }
                    
                }

            }
        });

    }
}
